#ifdef MBM_IMIBASE_SUPPORT

#include "mbm_spreadtrum_inc.h"

#include "mbm_typedefine.h"
#include "mbm_memory_adapter.h"
#include "mbm_sys_adapter.h"

//#define MBM_DEBUG_MEM

#if defined(MBM_DEBUG_MEM)
#define MEM_TRACE mbm_trace
#else
#define MEM_TRACE 
#endif

#if 1
extern void imp_mem_test_malloc(void** p, Int32 size, ImpCStr file, UInt32 line);
extern ImpBool imp_mem_test_free(void **ptr);
extern Int32 imp_mem_test_size(Int32 size);
#endif

void *mbm_malloc_ext_mem(Int32 size, ImpCStr file, UInt32 line)
{
	char* p;

    size = imp_mem_test_size(size);
    
	p = SCI_ALLOC_APP(size);
	SCI_ASSERT(p != 0);

    MEM_TRACE("mbm_malloc_ext_mem p=0x%x, size=%d, file:%s, line=%d", p, size, file, line);
	imp_mem_test_malloc(&p, size, file, line);

	return p;
}

void mbm_free_ext_mem(void **p)
{
	MEM_TRACE("mbm_free_ext_mem p=0x%x", *p);

    SCI_ASSERT(p != PNULL);
    imp_mem_test_free(p);
	SCI_Free(*p);
	*p = PNULL;
}

void *mbm_malloc_int_mem(Int32 size)
{
    MEM_TRACE("mbm_malloc_int_mem NULL");
	return NULL;
}

void mbm_free_int_mem(void **p)
{
	SCI_ASSERT(0);
}

UInt32 mbm_ext_mem_left_size(void)
{
	//CONST_HEAP_MEMORY
	mbm_trace("--RES const left: %d", SCI_GetByteHeapAvailableSpace(CONST_HEAP_MEMORY));
	mbm_trace("--RES base left: %d", SCI_GetByteHeapAvailableSpace(DYNAMIC_HEAP_BASE_MEMORY));
	mbm_trace("--RES app left: %d", SCI_GetByteHeapAvailableSpace(DYNAMIC_HEAP_APP_MEMORY));
	
	return SCI_GetByteHeapAvailableSpace(DYNAMIC_HEAP_APP_MEMORY);
}

UInt32 mbm_int_mem_left_size(void)
{
	SCI_ASSERT(0);
	return 0;
}

Int32 mbm_vsscanf(ImpCStr str, ImpCStr fmt, va_list params)
{
	ImpCStr pfmt = fmt;
	Int32 val_count = 0;
	void* val[10] = {0};
	Int32 i = 0;
	Int32 ret = 0;

	SCI_ASSERT(pfmt != NULL && str != NULL);
	
	//MEM_TRACE("mbm_vsscanf str=%s fmt=%s", str, fmt);
	while(*pfmt != 0)
	{
		if(*pfmt == '%')
		{			
			if(pfmt[1] == '%' || pfmt[1] == '*' || pfmt[1] == '\0')
			{
				pfmt++;
				continue;
			}
			val_count++;
		}
		pfmt++;
	}

	if(val_count > 10)
	{
		MEM_TRACE("mbm_vsscanf val too many val_count=%d", val_count);
		val_count = 10;
	}

	for(i = 0; i < val_count; i++)
	{
		val[i] = va_arg(params, void *);
		if(NULL == val[i])
		{
			break;
		}
	}

	switch(i)
	{
		case 1:
			ret = sscanf(str, fmt, val[0]);
			break;
		case 2:
			ret = sscanf(str, fmt, val[0], val[1]);
			break;
		case 3:
			ret = sscanf(str, fmt, val[0], val[1], val[2]);
			break;
		case 4:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3]);
			break;
		case 5:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4]);
			break;
		case 6:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5]);
			break;
		case 7:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6]);
			break;
		case 8:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
			break;
		case 9:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8]);
			break;
		case 10:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9]);
			break;
		default:
			break;
	}
	
	//MEM_TRACE("mbm_vsscanf i=%d, ret=%d", i, ret);
	return ret;
}

#endif /* MBM_IMIBASE_SUPPORT */
